package com.freesun.shop.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.freesun.shop.entity.ProdTag;
import com.freesun.shop.entity.ProdTagReference;
import com.freesun.shop.mapper.ProdTagMapper;

import com.freesun.shop.mapper.ProdTagReferenceMapper;
import com.freesun.shop.service.ProdTagService;
import org.apache.dubbo.config.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 商品分组表 服务实现类
 * </p>
 *
 * @author zf
 * @since 2019-11-13
 */
@Service
@CacheConfig(cacheNames = "com.freesun.shop.entity.ProdTag")
public class ProdTagServiceImpl extends ServiceImpl<ProdTagMapper, ProdTag> implements ProdTagService {
    private static Logger log= LoggerFactory.getLogger(ProdTagServiceImpl.class);
    @Autowired
    private ProdTagReferenceMapper prodTagReferenceMapper;
    @Override
    public IPage<ProdTag> findByPage(ProdTag prodTag, Page<ProdTag> page) {
        log.info("分页条件查询,当前页:{},每页显示大小:{}",page.getCurrent(),page.getSize());

        IPage<ProdTag> iPage = this.baseMapper.selectPage(page, new LambdaQueryWrapper<ProdTag>()
                .like(StringUtils.hasText(prodTag.getTitle()), ProdTag::getTitle, prodTag.getTitle())
                .eq(prodTag.getStatus() != null, ProdTag::getStatus, prodTag.getStatus())
                .ne(prodTag.getStatus() == null, ProdTag::getStatus, -1));
        return iPage;
    }

    @Override
    @Transactional
    @CacheEvict(key = INDEX_PROD_TAG)
    public boolean removeById(Serializable id) {
        ProdTag prodTagDb = getById(id);
        if(null==prodTagDb){
            throw new RuntimeException("要删除的数据不存在");
        }
        prodTagDb.setStatus(-1);
        prodTagDb.setDeleteTime(LocalDateTime.now());
        int result = this.baseMapper.updateById(prodTagDb);
        //删除中间关系表
        prodTagReferenceMapper.delete(new LambdaQueryWrapper<ProdTagReference>()
                .eq(ProdTagReference::getTagId, id));
        return result>0;
    }

    @Override
    public List<ProdTag> listProdTags() {
        log.info("开始查询商品的标签数据");
        return this.baseMapper.selectList(new LambdaQueryWrapper<ProdTag>()
        .eq(ProdTag::getStatus,1));
    }

    /**
     * 加载微信小程序的商品标签
     *
     * @return
     */
    private static final String INDEX_PROD_TAG = "'index-prod-tag-data'";
    @Override
    @Cacheable(key = INDEX_PROD_TAG)
    public List<ProdTag> getProdTagList() {
        return this.baseMapper.selectList(new LambdaQueryWrapper<ProdTag>()
                .eq(ProdTag::getStatus, 1)
                .orderByAsc(ProdTag::getSeq));
    }

    @Override
    @CacheEvict(key = INDEX_PROD_TAG)
    public boolean save(ProdTag entity) {
        return super.save(entity);
    }

    @Override
    @CacheEvict(key = INDEX_PROD_TAG)
    public boolean updateById(ProdTag entity) {
        return super.updateById(entity);
    }
}
